package com.jn.exam.oss.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jn.exam.R;
import com.jn.exam.oss.entity.FaceViolation;
import com.jn.exam.oss.entity.Student;
import com.jn.exam.oss.service.FaceViolationService;
import com.jn.exam.oss.service.OssService;
import com.jn.exam.oss.service.UcenterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

/**
 * <p>
 * 人脸违规记录 前端控制器
 * </p>
 *
 * @author 江南大学1033190417
 * @since 2022-03-14
 */
@RestController
@RequestMapping("/examoss/face-violation")
@Api(description = "人脸违规相关")
public class FaceViolationController {

    @Autowired
    private FaceViolationService faceViolationService;

    @Autowired
    private UcenterService ucenterService;

    @ApiOperation("获取某个学生的人脸违规记录")
    @GetMapping("getStudentFaceViolation")
    public R getStudentFaceViolation(String examId, String studentId) {
        QueryWrapper<FaceViolation> wrapper = new QueryWrapper<>();
        wrapper.eq("exam_id", examId).eq("student_id", studentId).orderByDesc("gmt_create");
        List<FaceViolation> faceViolations = faceViolationService.list(wrapper);
        return R.ok().data("faceViolations", faceViolations);
    }

    @ApiOperation("删除一个或者多个人脸违规记录")
    @PostMapping("deleteFaceViolation")
    public R deleteFaceViolation(String[] faceViolationIds) throws Exception {
        boolean b = removeFaceViolationByIds(Arrays.asList(faceViolationIds));
        if (b) {
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    @ApiOperation("删除一个学生的人脸违规")
    @DeleteMapping("deleteStudentFaceViolation")
    public R deleteStudentFaceViolation(String examId, String studentId) throws Exception {
        QueryWrapper<FaceViolation> wrapper = new QueryWrapper<>();
        wrapper.eq("exam_id", examId).eq("student_id", studentId).select("id");
        List<String> faceViolationIds = faceViolationService.list(wrapper).stream().map(FaceViolation::getId).collect(Collectors.toList());
        boolean b = removeFaceViolationByIds(faceViolationIds);
        if (b) {
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }


    @ApiOperation("查询某考试所有人脸违规的学生")
    @PostMapping("getAllFaceViolationStudents")
    public R getAllFaceViolationStudents(String examId) {
        QueryWrapper<FaceViolation> wrapper = new QueryWrapper<>();
        wrapper.eq("exam_id", examId).select("student_id");
        List<String> studentIds = faceViolationService.list(wrapper).stream().map(FaceViolation::getStudentId).distinct().collect(Collectors.toList());
        if (studentIds == null || studentIds.size() == 0) {
            return R.ok().data("students", null);
        }
//        String name = student.getName();
//        String number = student.getNumber();
//        Integer age = student.getAge();
//        Integer sex = student.getSex();
//        if (!StringUtils.isEmpty(name)){
//            wrapper1.like("name", name);
//        }
//        if (!StringUtils.isEmpty(number)){
//            wrapper1.eq("number",number);
//        }
//        if (age!=null){
//            wrapper1.eq("age",age);
//        }
//        if (sex != null) {
//            wrapper1.eq("sex",sex);
//        }
//        Page<Student> page=new Page<>();
//        page.setCurrent(current).setSize(limit);
//        studentService.page(page,wrapper1);
//        long total = page.getTotal();
//        List<Student> records = page.getRecords();
        return ucenterService.getStudentInIds(studentIds);
    }


    private boolean removeFaceViolationByIds(List<String> faceViolationIds) throws Exception {
        List<Future<Boolean>> futures = new ArrayList<>();
        for (String faceViolationId : faceViolationIds) {
            Future<Boolean> future = faceViolationService.removeFaceViolationById(faceViolationId);
            futures.add(future);
        }
        for (Future<Boolean> future : futures) {
            Boolean b = future.get();
            if (!b) {
                return false;
            }
        }
        return true;
    }

}

